<template>
    <el-dialog v-if="dialogVisible" :close-on-click-modal='false' title="重命名" :visible.sync="dialogVisible" @close="resetForm('editFunForm')" width="400px">
        <el-form label-position="top" :model="editFunForm" :hide-required-asterisk='true' ref="editFunForm" :rules="editFunRules" class="dialog_form">
            <el-form-item label="职能名称" prop="name">
                <el-input v-model="editFunForm.name" placeholder="请输入职能名称"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button size="small" @click="resetForm('editFunForm')" class="el-button-width">取 消</el-button>
            <el-button size="small" type="primary" @click="submitForm('editFunForm')" :loading="loading" :disabled="loading" class="el-button-width">保 存</el-button>
        </span>
    </el-dialog>
</template>

<script>
export default {
    name: "",
    data() {
        return {
            loading: false,
            dialogVisible: false,
            editFunForm: {
                name: "",
                id:''
            },
            editFunRules: {
                name: [
                    { required: true, message: "请输入职能名称", trigger: ['blur', 'change'] },
                ],
            },
        };
    },
    created() {
        this.$bus.on("editFun", (value) => {
            this.dialogVisible = value.dialogVsb;
            this.editFunForm.name = value.name;
            this.editFunForm.id = value.id;
        });
    },
    methods: {
        // 提交添加
        submitForm(formName) {
            this.$refs[formName].validate(async (valid) => {
                if (valid) {
                    this.loading = true;
                    const { data: res } = await this.$http.post(
                        "adminapi/company/functionEdit",
                        this.editFunForm
                    );
                    if (res.code !== 200) {
                        this.$message.error(res.msg);
                        this.loading = false;
                        return;
                    } else {
                        this.$message({
                            message: "修改成功",
                            type: "success",
                        });
                        this.loading = false;
                        this.resetForm(formName);
                        this.$parent.getFuntList();
                    }
                } else {
                    this.loading = false;
                    return false;
                }
            });
        },

        // 关闭重置表单
        resetForm(formName) {
            this.$refs[formName].resetFields();
            this.dialogVisible = false;
        },
    },
    components: {},
};
</script>


<style scoped>
</style>
